package com.aabte.commons.errorcodetest;

import com.aabte.commons.bean.BaseResponse;
import com.aabte.commons.errorcode.ResponseFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * @author Daniel
 * @version 1.0
 * @date 2020/4/30
 */
@RestController
@RequestMapping(path = "/user")
@SpringBootApplication
public class Bootstrap {

  public static void main(String[] args) {
    SpringApplication.run(Bootstrap.class, args);
  }

  @Value("${spring.application.name:''}")
  private String applicationName;

  @PostConstruct
  public void setup() {
    BaseResponse.setProviderId(applicationName);
  }

  @Resource
  private MockService mockService;

    @GetMapping(path = "/{id}")
    public BaseResponse<UserDTO> getUser(@PathVariable(name = "id") Long id) {
        UserDTO user = mockService.getUser(id);
        return ResponseFactory.success(user);
    }

    @PostMapping(path = "")
    public BaseResponse<UserDTO> addUser(@Validated @RequestBody UserParam userParam) {
        UserDTO user = mockService.addUser(userParam);
        return ResponseFactory.success(user);
    }
}
